package com.wzp.yyptproject.controller;


import com.wzp.yyptproject.entity.*;
import com.wzp.yyptproject.repository.EkBingRepository;
import com.wzp.yyptproject.repository.PingLunRepository;
import com.wzp.yyptproject.service.EkBingService;
import com.wzp.yyptproject.service.PingLunService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("pinglun")
public class PingLunController {
    @Autowired
    private PingLunService pingLunService;
    @Autowired
    private PingLunRepository pingLunRepository;
    @GetMapping("findbyuid")
    public  List<PingLun>  findByUid(Integer uid){
        List<PingLun> pingLun = pingLunService.finfByUid(uid);
            return pingLun;

    }
    @PostMapping("findallmypinglun")
    public  List<PingLun2> findAllMyPingLun(@RequestBody User user){
        System.out.println(22222);
        List<PingLun> pingLun = pingLunService.findAllMyPingLun(user.getUname(),user.getUidcard());
        List<PingLun2> pingLunList = pingLunService.findAllPinglun1(pingLun);
        return pingLunList;

    }
    @GetMapping("findallpinglun")
    public  List<PingLun2> findAllPingLun(){
        List<PingLun2> pingLunList = pingLunService.findAllPinglun1(pingLunService.findAllByStatus());
        return pingLunList;

    }

    @PostMapping("addpinglun")
    public String addpinglun(@RequestBody PingLun2 pingLun2){
        if (pingLunService.addPinglun(pingLun2.getUidcard(),pingLun2.getUname(),pingLun2.getDDid(),pingLun2.getPdate(),pingLun2.getPxingji(),
                pingLun2.getPcontent())==true){
            return "success";
        }
       return "failed";

    }

    @GetMapping("/findByPage")
    public Page<PingLun2> findByPage(@RequestParam(value = "pageCode" ,defaultValue = "0")int pageCode,
                                    @RequestParam(value = "pageSize" ,defaultValue = "4")int pageSize) {
        log.info("页码"+pageCode);
        log.info("条数"+pageSize);
        Page<PingLun2> pingLun2Page= pingLunService.findByPage(pageCode,pageSize);
        System.out.println("数据"+pingLun2Page);
        System.out.println("总数"+pingLun2Page.getTotalElements());
        return pingLun2Page;

//        return userService.findByPage(pageEntity.getPageCode(),pageEntity.getPageSize());
    }
    @GetMapping("findallcount")
    public Integer findAllCount(){
        return pingLunService.findAllCount();

    }
    @GetMapping("deletepinglun")
    public String deletePingLun(Integer pid){
        if (pingLunService.deleteByPid(pid)==true){
            return "success";
        }
        return "failed";

    }
}



